/*
** Copyright (C) 2011 Viens Yann
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <opencv/highgui.h>
#include "Rocket.hpp"
#include "Variables.hpp"

IplImage*		Rocket::_sprite = NULL;

Rocket::Rocket()
{
	if (_sprite == NULL)
		_sprite = cvLoadImage(ROCKETPATH);
	initPos();
}

Rocket::~Rocket()
{
}

void			Rocket::initPos()
{
  if (_sprite)
    {
      _size._x = _sprite->width;
      _size._y = _sprite->height;
    }
  else
    {
      _size._x = DEFAULTROCKETWIDTH;
      _size._y = DEFAULTROCKETHEIGHT;
    }
  _dir._x = 0;
  _dir._y = (rand() % 10) + 5;

  _pos._x = (rand() % (camZoneWidth - 2 * _size._x) + _size._x);
  _pos._y = 0;
}

void					Rocket::draw(IplImage* screen)
{
  if (_sprite)
    {
      int			x, y, beginX = _pos._x, beginY = _pos._y;
      CvScalar	scalar;

      for (y = 0; y < _size._y; y++)
	{
	  for (x = 0; x < _size._x; x++)
	    {
	      scalar = cvGet2D(_sprite, y, x);
	      if (!(scalar.val[0] == 255 && scalar.val[1] == 0 && scalar.val[2] == 255) && beginY + y < camZoneHeight && beginX + x < camZoneWidth)
		cvSet2D(screen, beginY + y, beginX + x, scalar);
	    }
	}
    }
}
